

##################################################################
##################################################################
## Replication Material
##
## Widmann & Simonsen: 
## Setting the Tone: The Diffusion of Moral and Moral-Emotional Appeals 
## across Political and Public Discourse
##
## Political Science Research & Methods (PSRM)
##
##
##################################################################
##################################################################

# Note: The file 000_readme.pdf describes all scripts and datasets required to replicate the analysis

# This script was run on the following R version, platform and OS:
# R version 4.4.0 (2024-04-24)
# Platform: aarch64-apple-darwin20
# Running under: macOS Sonoma 14.3.1


sessionInfo()

# Custom functions --------------------------------------------------------

create_coef_plot_simple <- function(results) {
  results$Source <- "Day7"
  combined_results <- results
  
  # Filter the combined results to obtain relevant groups
  filtered_results <- combined_results[combined_results$IRF == 3 | combined_results$IRF == 5, ]
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="politics_morality"] <- "Politics"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="politics_morality"] <- "Politics"
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="polinterest_morality"] <- "polinterest"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="polinterest_morality"] <- "polinterest"
  
  filtered_results$ImpulseGroup <- as.factor(filtered_results$ImpulseGroup)
  filtered_results$ResponseGroup <- as.factor(filtered_results$ResponseGroup)
  filtered_results$ResponseGroup <- factor(filtered_results$ResponseGroup, levels = c("Politics", "polinterest"), 
                                           labels = c("Politics", "Public"))
  filtered_results$ImpulseGroup <- factor(filtered_results$ImpulseGroup, 
                                          levels = c("Politics", "polinterest"), 
                                          labels = c("Politics", "Public"))
  
  # Create the coefficient plot with flipped axes and facet_wrap
  coef_plot <- ggplot(filtered_results, aes(y = ImpulseGroup, x = Coefficient, xmin = LowerBound, xmax = UpperBound, color = Source)) +
    geom_pointrange(position = position_dodge(width = 0.5), colour = "red") +
    geom_vline(xintercept = 0, linetype = "dashed", color = "black", size = 0.3) +
    labs(x = "Coefficient",
         y = "Impulse Group",
         title = "") +
    xlim(-0.04, 0.09) +
    guides(color = "none") +
    facet_wrap(~ResponseGroup, ncol = 2) +
    theme_minimal()
  return(coef_plot)
}



create_coef_plot_simple2 <- function(results1, results2, results3, results4) {

  results1$Source <- "Day7"
  
  # Filter the combined results to obtain relevant groups
  filtered_results <- results1[results1$IRF == 3 | results1$IRF == 5, ]
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="politics_morality"] <- "Politics -> Public"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="politics_morality"] <- "Moral positive"
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="polinterest_morality"] <- "Public -> Politics"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="polinterest_morality"] <- "Moral positive"
  
  filtered_results$ImpulseGroup <- as.factor(filtered_results$ImpulseGroup)
  filtered_results$ResponseGroup <- as.factor(filtered_results$ResponseGroup)
  
  combined_results <- filtered_results
  
  results2$Source <- "Day7"
  
  # Filter the combined results to obtain relevant groups
  filtered_results <- results2[results2$IRF == 3 | results2$IRF == 5, ]
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="politics_morality"] <- "Politics -> Public"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="politics_morality"] <- "Moral negative"
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="polinterest_morality"] <- "Public -> Politics"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="polinterest_morality"] <- "Moral negative"
  
  filtered_results$ImpulseGroup <- as.factor(filtered_results$ImpulseGroup)
  filtered_results$ResponseGroup <- as.factor(filtered_results$ResponseGroup)
  
  combined_results <- rbind(combined_results, filtered_results)
  
  results3$Source <- "Day7"
  
  # Filter the combined results to obtain relevant groups
  filtered_results <- results3[results3$IRF == 3 | results3$IRF == 5, ]
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="politics_morality"] <- "Politics -> Public"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="politics_morality"] <- "Moral-emotional positive"
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="polinterest_morality"] <- "Public -> Politics"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="polinterest_morality"] <- "Moral-emotional positive"
  
  filtered_results$ImpulseGroup <- as.factor(filtered_results$ImpulseGroup)
  filtered_results$ResponseGroup <- as.factor(filtered_results$ResponseGroup)
  
  combined_results <- rbind(combined_results, filtered_results)
  
  results4$Source <- "Day7"
  
  # Filter the combined results to obtain relevant groups
  filtered_results <- results4[results4$IRF == 3 | results4$IRF == 5, ]
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="politics_morality"] <- "Politics -> Public"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="politics_morality"] <- "Moral-emotional negative"
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="polinterest_morality"] <- "Public -> Politics"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="polinterest_morality"] <- "Moral-emotional negative"
  
  # Assuming 'impulse_group' and 'response_group' are columns in filtered_results
  filtered_results$ImpulseGroup <- as.factor(filtered_results$ImpulseGroup)
  filtered_results$ResponseGroup <- as.factor(filtered_results$ResponseGroup)
  
  combined_results <- rbind(combined_results, filtered_results)
  
  combined_results$ResponseGroup <- factor(combined_results$ResponseGroup, levels = c("Moral-emotional negative", "Moral-emotional positive","Moral negative", "Moral positive"))
  
  # Create the coefficient plot with flipped axes and facet_wrap
  coef_plot <- ggplot(combined_results, aes(y = ResponseGroup, x = Coefficient, xmin = LowerBound, xmax = UpperBound, color = Source)) +
    geom_pointrange(position = position_dodge(width = 0.5), colour = "black") +
    geom_vline(xintercept = 0, linetype = "dashed", color = "red", size = 0.3) +
    xlim(-0.04, 0.12) +
    guides(color = "none") +
    facet_wrap(~ImpulseGroup, ncol = 2) +
    theme_minimal() +
    theme(axis.title.x = element_blank(),  # Remove x-axis label
          axis.title.y = element_blank(),
          text = element_text(size = 16))
  return(coef_plot)
}





svar_simple <- function(emotional_appeal, p_lags) {
  p <- p_lags
  newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
  newspaper_tweets_second <- newspaper_tweets |> 
    filter(date >= "2017-09-25")
  
  politics_second <- politics |> 
    filter(date >= "2017-09-25")
  
  polinterest$date <- as.Date(polinterest$created_at)
  polinterest_second <- polinterest |> 
    filter(date >= "2017-09-25")
  
  newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[emotional_appeal]], list(newspaper_tweets_second$date), mean)
  colnames(newspaper_tweets_agg)[1] <- "date"
  colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
  
  politics_agg <- aggregate(politics_second[[emotional_appeal]], list(politics_second$date), mean)
  colnames(politics_agg)[1] <- "date"
  colnames(politics_agg)[2] <- "politics_morality"
  
  polinterest_agg <- aggregate(polinterest_second[[emotional_appeal]], list(polinterest_second$date), mean)
  colnames(polinterest_agg)[1] <- "date"
  colnames(polinterest_agg)[2] <- "polinterest_morality"
  
  newspaper_tweets_agg <- newspaper_tweets_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  politics_agg <- politics_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  polinterest_agg <- polinterest_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  # Create a sequence of dates
  date_sequence <- seq(from = as.Date("2017-09-24"), to = as.Date("2022-01-01"), by = "day")
  
  # Create a dataframe using the date sequence
  var_dataframe <- tibble(date = date_sequence)
  
  var_dataframe <- merge(var_dataframe, newspaper_tweets_agg, all.x = TRUE)
  var_dataframe <- merge(var_dataframe, politics_agg, all.x = TRUE)
  var_dataframe <- merge(var_dataframe, polinterest_agg, all.x = TRUE)
  
  #set NAs to 0
  var_dataframe[is.na(var_dataframe)] <- 0
  var_dataframe$date <- NULL
  
  var <- VAR(var_dataframe, p = p, type = "const")
  svar_model <- BQ(var)
  
  svar.1 <- irf(svar_model, n.ahead = 7,impulse = "newspaper_tweets_morality", response = "polinterest_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
  
  svar.2 <- irf(svar_model, n.ahead = 7,impulse = "newspaper_tweets_morality", response = "politics_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
  
  svar.3 <- irf(svar_model, n.ahead = 7,impulse = "politics_morality", response = "polinterest_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)  
  
  svar.4 <- irf(svar_model, n.ahead = 7,impulse = "politics_morality", response = "newspaper_tweets_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)  
  
  svar.5 <- irf(svar_model, n.ahead = 7,impulse = "polinterest_morality", response = "politics_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)  
  
  svar.6 <- irf(svar_model, n.ahead = 7,impulse = "polinterest_morality", response = "newspaper_tweets_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)  
  
  
  # Initialize a data frame to store the results2
  results2 <- data.frame(IRF = numeric(), Coefficient = numeric(),
                         LowerBound = numeric(), UpperBound = numeric(),
                         Significant = logical(), Day = integer(),
                         ImpulseGroup = character(), ResponseGroup = character())
  
  # Number of IRFs
  num_irfs <- 6
  
  # Loop through all the IRFs
  for (i in 1:num_irfs) {
    # Get the IRF object
    irf_obj <- get(paste("svar", i, sep = "."))
    
    # Extract the impulse and response groups for this IRF
    impulse_group <- irf_obj$impulse
    response_group <- irf_obj$response
    
    # Extract the coefficients for days 1 to 7
    coef_days1to7 <- irf_obj$irf[[1]][1:8,]
    
    # Extract the lower and upper bounds for days 1 to 7
    lower_bounds_days1to7 <- irf_obj$Lower[[1]][1:8,]
    upper_bounds_days1to7 <- irf_obj$Upper[[1]][1:8,]
    
    # Check if the coefficients are significantly different from 0
    significant_days <- (lower_bounds_days1to7 > 0) | (upper_bounds_days1to7 < 0)
    
    # Filter significant coefficients
    significant_coefs <- coef_days1to7[significant_days]
    significant_lowers <- lower_bounds_days1to7[significant_days]
    significant_uppers <- upper_bounds_days1to7[significant_days]
    
    if (length(significant_coefs) > 0) {
      if (all(significant_coefs >= 0)) {
        index <- which.max(significant_coefs)
      } else if (all(significant_coefs <= 0)) {
        index <- which.min(significant_coefs)
      } else {
        index <- which.max(abs(significant_coefs))
      }
      
      # Add the results2 to the data frame
      results2 <- rbind(results2, data.frame(IRF = i, Coefficient = significant_coefs[index],
                                             LowerBound = significant_lowers[index], UpperBound = significant_uppers[index],
                                             Significant = TRUE, Day = which(significant_days)[index],
                                             ImpulseGroup = impulse_group, ResponseGroup = response_group))
    } else {
      # If no significant coefficients are found, add the row with the largest absolute coefficient
      index <- which.max(abs(coef_days1to7))
      results2 <- rbind(results2, data.frame(IRF = i, Coefficient = coef_days1to7[index],
                                             LowerBound = lower_bounds_days1to7[index], UpperBound = upper_bounds_days1to7[index],
                                             Significant = FALSE, Day = index,
                                             ImpulseGroup = impulse_group, ResponseGroup = response_group))
    }
  }
  
  # Display the results2
  return(results2)
}




svar_complex_complete_binary <- function(emotional_appeals, p_lags) {
  for (emotional_appeal in emotional_appeals) {
    p <- p_lags
    newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
    newspaper_tweets_second <- newspaper_tweets |> 
      filter(date >= "2017-09-25")
    
    politics_second <- politics |> 
      filter(date >= "2017-09-25")
    
    polinterest$date <- as.Date(polinterest$created_at)
    polinterest_second <- polinterest |> 
      filter(date >= "2017-09-25")
    
    partytype_agg <- aggregate(politics_second[[emotional_appeal]], list(politics_second$date, politics_second$afd), mean)
    colnames(partytype_agg)[1] <- "date"
    colnames(partytype_agg)[2] <- "partytype"
    partytype_wide <- partytype_agg %>% spread(partytype, x)
    
    newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[emotional_appeal]], list(newspaper_tweets_second$date), mean)
    colnames(newspaper_tweets_agg)[1] <- "date"
    colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
    
    polinterest_agg <- aggregate(polinterest_second[[emotional_appeal]], list(polinterest_second$date), mean)
    colnames(polinterest_agg)[1] <- "date"
    colnames(polinterest_agg)[2] <- "polinterest_morality"
    
    date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")
    
    # Create a dataframe using the date sequence
    var_complete <- tibble(date = date_sequence)
    
    var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
    var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
    var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)
    
    #set NAs to 0
    var_complete[is.na(var_complete)] <- 0
    var_complete$date <- NULL
    
    var <- VAR(var_complete, p = p, type = "const")
    svar_model <- BQ(var)
    
    
    svar.13 <- irf(svar_model, n.ahead = 7,impulse = "AfD", response = "polinterest_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.14 <- irf(svar_model, n.ahead = 7,impulse = "Other.Parties", response = "polinterest_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.15 <- irf(svar_model, n.ahead = 7,impulse = "polinterest_morality", response = "AfD", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.16 <- irf(svar_model, n.ahead = 7,impulse = "polinterest_morality", response = "Other.Parties", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    
    # Initialize a data frame to store the results2
    results2 <- data.frame(IRF = numeric(), Coefficient = numeric(),
                           LowerBound = numeric(), UpperBound = numeric(),
                           Significant = logical(), Day = integer(),
                           ImpulseGroup = character(), ResponseGroup = character())
    
    # Number of IRFs
    num_irfs <- 4
    
    # Loop through all the IRFs
    for (i in 13:16) {
      # Get the IRF object
      irf_obj <- get(paste("svar", i, sep = "."))
      
      # Extract the impulse and response groups for this IRF
      impulse_group <- irf_obj$impulse
      response_group <- irf_obj$response
      
      # Extract the coefficients for days 1 to 7
      coef_days1to7 <- irf_obj$irf[[1]][1:8,]
      
      # Extract the lower and upper bounds for days 1 to 7
      lower_bounds_days1to7 <- irf_obj$Lower[[1]][1:8,]
      upper_bounds_days1to7 <- irf_obj$Upper[[1]][1:8,]
      
      # Check if the coefficients are significantly different from 0
      significant_days <- (lower_bounds_days1to7 > 0) | (upper_bounds_days1to7 < 0)
      
      # Filter significant coefficients
      significant_coefs <- coef_days1to7[significant_days]
      significant_lowers <- lower_bounds_days1to7[significant_days]
      significant_uppers <- upper_bounds_days1to7[significant_days]
      
      if (length(significant_coefs) > 0) {
        if (all(significant_coefs >= 0)) {
          index <- which.max(significant_coefs)
        } else if (all(significant_coefs <= 0)) {
          index <- which.min(significant_coefs)
        } else {
          index <- which.max(abs(significant_coefs))
        }
        
        # Add the results2 to the data frame
        results2 <- rbind(results2, data.frame(IRF = i, Coefficient = significant_coefs[index],
                                               LowerBound = significant_lowers[index], UpperBound = significant_uppers[index],
                                               Significant = TRUE, Day = which(significant_days)[index],
                                               ImpulseGroup = impulse_group, ResponseGroup = response_group))
      } else {
        # If no significant coefficients are found, add the row with the largest absolute coefficient
        index <- which.max(abs(coef_days1to7))
        results2 <- rbind(results2, data.frame(IRF = i, Coefficient = coef_days1to7[index],
                                               LowerBound = lower_bounds_days1to7[index], UpperBound = upper_bounds_days1to7[index],
                                               Significant = FALSE, Day = index,
                                               ImpulseGroup = impulse_group, ResponseGroup = response_group))
      }
    }
    
    results2$appeal <- emotional_appeal
    # Display the results2
    complex_complete <- rbind(complex_complete, results2)
    
    
  }
  
  return(complex_complete)
}



create_coef_plot_complex_complete_binary <- function(results) {
  
  results$Source <- "Day 7"
  combined_results <- results
  
  # Filter the combined results to obtain relevant groups
  filtered_results <- combined_results[combined_results$IRF >= 13 & combined_results$IRF <= 14, ]
  
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="polinterest_morality"] <- "polinterest"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="polinterest_morality"] <- "polinterest"
  
  # Set the order for ImpulseGroup
  filtered_results$ImpulseGroup <- factor(filtered_results$ImpulseGroup, 
                                          levels = rev(c("polinterest", "AfD", "Other.Parties")), 
                                          labels = rev(c("Public", "Radical right", "Other parties")))
  
  # Set the order for ResponseGroup
  filtered_results$ResponseGroup <- factor(filtered_results$ResponseGroup, 
                                           levels = rev(c("polinterest", "AfD", "Other.Parties")), 
                                           labels = rev(c("Public", "Radical right", "Other parties")))
  
  
  # Set the order for appeals
  filtered_results$appeal <- factor(filtered_results$appeal, 
                                    levels = rev(c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")), 
                                    labels = rev(c("M positive", "M negative", "ME positive", "ME negative")))
  
  filtered_results$ImpulseGroup <- as.factor(filtered_results$ImpulseGroup)
  filtered_results$ResponseGroup <- as.factor(filtered_results$ResponseGroup)
  filtered_results$appeal <- as.factor(filtered_results$appeal)
  
  # Change 'Significant' from logical to character to prevent issues with ggplot
  filtered_results$Significant <- as.character(filtered_results$Significant)
  
  # Mapping of 'appeal' values to titles
  title_map <- c(
    morality_only_pos = "1) Moral positive",
    morality_only_neg = "2) Moral negative",
    pos_moremo = "3) Moral-emotional positive",
    neg_moremo = "4) Moral-emotional negative"
  )
  plot_title <- title_map[filtered_results$appeal[1]]
  
  # Plot
  coef_plot1 <- ggplot(filtered_results, aes(y = ImpulseGroup, x = Coefficient, group = appeal)) +
    geom_linerange(aes(xmin = Coefficient, xmax = UpperBound), position = position_dodge(width = 0.5), colour = "black") +
    geom_linerange(aes(xmin = LowerBound, xmax = Coefficient), position = position_dodge(width = 0.5), colour = "black") +
    geom_point(aes(shape = appeal), position = position_dodge(width = 0.5), colour = "black", size = 2) +
    geom_vline(xintercept = 0, linetype = "dashed", color = "red", size = 0.3) +
    xlim(-0.1, 0.15) +
    facet_wrap(~ResponseGroup, ncol = 3) +
    theme_minimal() +
    theme(axis.title.x = element_blank(),  # Remove x-axis label
          axis.title.y = element_blank(),
          text = element_text(size = 14),
          legend.position = "none") +  # Make sure you want the legend hidden
    scale_shape_manual(values = c(15, 17, 18, 20), name = "Appeal") +
    guides(shape = guide_legend(reverse = TRUE))  # Reverse the legend order
  
  
  
  filtered_results <- combined_results[combined_results$IRF >= 15 & combined_results$IRF <= 16, ]
  
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="polinterest_morality"] <- "polinterest"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="polinterest_morality"] <- "polinterest"
  
  
  # Set the order for ImpulseGroup
  filtered_results$ImpulseGroup <- factor(filtered_results$ImpulseGroup, 
                                          levels = rev(c("polinterest", "AfD", "Other.Parties")), 
                                          labels = rev(c("Public", "Radical right", "Other parties")))
  
  # Set the order for ResponseGroup
  filtered_results$ResponseGroup <- factor(filtered_results$ResponseGroup, 
                                           levels = rev(c("polinterest", "AfD", "Other.Parties")), 
                                           labels = rev(c("Public", "Radical right", "Other parties")))
  
  
  
  # Set the order for ResponseGroup
  filtered_results$appeal <- factor(filtered_results$appeal, 
                                    levels = rev(c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")), 
                                    labels = rev(c("M positive", "M negative", "ME positive", "ME negative")))
  
  
  
  # Assuming 'impulse_group' and 'response_group' are columns in filtered_results
  filtered_results$ImpulseGroup <- as.factor(filtered_results$ImpulseGroup)
  filtered_results$ResponseGroup <- as.factor(filtered_results$ResponseGroup)
  filtered_results$appeal <- as.factor(filtered_results$appeal)
  
  
  # Change 'Significant' from logical to character to prevent issues with ggplot
  filtered_results$Significant <- as.character(filtered_results$Significant)
  
  # Assuming 'ResponseGroup' is the faceting variable
  filtered_results$ResponseGroup <- factor(filtered_results$ResponseGroup, 
                                           levels = c("Radical right", "Other parties"))
  
  
  # Plot
  coef_plot2 <- ggplot(filtered_results, aes(y = ImpulseGroup, x = Coefficient, group = appeal)) +
    geom_linerange(aes(xmin = Coefficient, xmax = UpperBound), position = position_dodge(width = 0.5), colour = "black") +
    geom_linerange(aes(xmin = LowerBound, xmax = Coefficient), position = position_dodge(width = 0.5), colour = "black") +
    geom_point(aes(shape = appeal), position = position_dodge(width = 0.5), colour = "black", size = 2) +
    geom_vline(xintercept = 0, linetype = "dashed", color = "red", size = 0.3) +
    xlim(-0.075, 0.05) +
    facet_wrap(~ResponseGroup, nrow = 5) +
    theme_minimal() +
    theme(axis.title.x = element_blank(),  # Remove x-axis label
          axis.title.y = element_blank(),
          text = element_text(size = 14)) +
    scale_shape_manual(values = c(15, 17, 18, 20), name = "Appeal") +
    guides(shape = guide_legend(reverse = TRUE))  # Reverse the legend order
  
  
  # Combine the two plots
  plot <- plot_grid(coef_plot1, coef_plot2, ncol = 2, rel_heights = 4)

  return(plot)
}



check_optimal_lags_simple <- function(emotional_appeal) {
  
  newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
  newspaper_tweets_second <- newspaper_tweets |> 
    filter(date >= "2017-09-25")
  
  politics_second <- politics |> 
    filter(date >= "2017-09-25")
  
  polinterest$date <- as.Date(polinterest$created_at)
  polinterest_second <- polinterest |> 
    filter(date >= "2017-09-25")
  
  newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[emotional_appeal]], list(newspaper_tweets_second$date), mean)
  colnames(newspaper_tweets_agg)[1] <- "date"
  colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
  
  politics_agg <- aggregate(politics_second[[emotional_appeal]], list(politics_second$date), mean)
  colnames(politics_agg)[1] <- "date"
  colnames(politics_agg)[2] <- "politics_morality"
  
  polinterest_agg <- aggregate(polinterest_second[[emotional_appeal]], list(polinterest_second$date), mean)
  colnames(polinterest_agg)[1] <- "date"
  colnames(polinterest_agg)[2] <- "polinterest_morality"
  
  newspaper_tweets_agg <- newspaper_tweets_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  politics_agg <- politics_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  polinterest_agg <- polinterest_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  # Create a sequence of dates 
  date_sequence <- seq(from = as.Date("2017-09-24"), to = as.Date("2022-01-01"), by = "day")
  
  # Create a dataframe using the date sequence
  var_dataframe <- tibble(date = date_sequence)
  
  var_dataframe <- merge(var_dataframe, newspaper_tweets_agg, all.x = TRUE)
  var_dataframe <- merge(var_dataframe, politics_agg, all.x = TRUE)
  var_dataframe <- merge(var_dataframe, polinterest_agg, all.x = TRUE)
  
  #set NAs to 0
  var_dataframe[is.na(var_dataframe)] <- 0
  var_dataframe$date <- NULL
  
  # Determine the optimal number of lags
  lags_optimal <- VARselect(var_dataframe, lag.max = 14, type = "const")$selection
  
  return(lags_optimal)
}


check_optimal_lags_complex_binary <- function(emotional_appeal) {
  newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
  newspaper_tweets_second <- newspaper_tweets |> 
    filter(date >= "2017-09-25")
  
  politics_second <- politics |> 
    filter(date >= "2017-09-25")
  
  polinterest$date <- as.Date(polinterest$created_at)
  polinterest_second <- polinterest |> 
    filter(date >= "2017-09-25")
  
  partytype_agg <- aggregate(politics_second$morality_only, list(politics_second$date, politics_second$afd), mean)
  colnames(partytype_agg)[1] <- "date"
  colnames(partytype_agg)[2] <- "partytype"
  partytype_wide <- partytype_agg %>% spread(partytype, x)
  
  newspaper_tweets_agg <- aggregate(newspaper_tweets_second$morality_only, list(newspaper_tweets_second$date), mean)
  colnames(newspaper_tweets_agg)[1] <- "date"
  colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
  
  polinterest_agg <- aggregate(polinterest_second$morality_only, list(polinterest_second$date), mean)
  colnames(polinterest_agg)[1] <- "date"
  colnames(polinterest_agg)[2] <- "polinterest_morality"
  
  # Create a sequence of dates
  date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")
  
  # Create a dataframe using the date sequence
  var_complete <- tibble(date = date_sequence)
  
  var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
  var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
  var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)
  
  
  #set NAs to 0
  var_complete[is.na(var_complete)] <- 0
  var_complete$date <- NULL
  
  # Determine the optimal number of lags
  lags_optimal <- VARselect(var_complete, lag.max = 14, type = "const")$selection
  return(lags_optimal)
}


# Define a function to get AIC and SC from check_optimal_lags_simple
get_aic_sc <- function(model_name) {
  results <- check_optimal_lags_simple(model_name)
  return(data.frame(AIC = results["AIC(n)"], SC = results["SC(n)"]))
}

# Define a function to get AIC and SC from check_optimal_lags_complex_binary
get_aic_sc_complex <- function(model_name) {
  results <- check_optimal_lags_complex_binary(model_name)
  return(data.frame(AIC = results["AIC(n)"], SC = results["SC(n)"]))
}


calculate_stats_simple <- function(emotional_appeal) {
  newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
  newspaper_tweets_second <- newspaper_tweets |> 
    filter(date >= "2017-09-25")
  
  politics_second <- politics |> 
    filter(date >= "2017-09-25")
  
  polinterest$date <- as.Date(polinterest$created_at)
  polinterest_second <- polinterest |> 
    filter(date >= "2017-09-25")
  
  newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[emotional_appeal]], list(newspaper_tweets_second$date), mean)
  colnames(newspaper_tweets_agg)[1] <- "date"
  colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
  
  politics_agg <- aggregate(politics_second[[emotional_appeal]], list(politics_second$date), mean)
  colnames(politics_agg)[1] <- "date"
  colnames(politics_agg)[2] <- "politics_morality"
  
  polinterest_agg <- aggregate(polinterest_second[[emotional_appeal]], list(polinterest_second$date), mean)
  colnames(polinterest_agg)[1] <- "date"
  colnames(polinterest_agg)[2] <- "polinterest_morality"
  
  newspaper_tweets_agg <- newspaper_tweets_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  politics_agg <- politics_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  polinterest_agg <- polinterest_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  # Create a sequence of dates
  date_sequence <- seq(from = as.Date("2017-09-24"), to = as.Date("2022-01-01"), by = "day")
  
  # Create a dataframe using the date sequence
  var_dataframe <- tibble(date = date_sequence)
  
  var_dataframe <- merge(var_dataframe, newspaper_tweets_agg, all.x = TRUE)
  var_dataframe <- merge(var_dataframe, politics_agg, all.x = TRUE)
  var_dataframe <- merge(var_dataframe, polinterest_agg, all.x = TRUE)
  
  #set NAs to 0
  var_dataframe[is.na(var_dataframe)] <- 0
  var_dataframe <- var_dataframe %>%
    mutate(week = floor_date(date, "week")) %>%
    group_by(week)
  
  var_dataframe <- var_dataframe %>%
    group_by(week) %>%
    summarise(newspaper_tweets_morality = mean(newspaper_tweets_morality),
              politics_morality = mean(politics_morality),
              polinterest_morality = mean(polinterest_morality))
  var_dataframe$date <- NULL
  
  # Calculate mean and SD for each group, returning them in a tibble
  result <- tibble(
    emotional_appeal = emotional_appeal,
    mean_politics_morality = mean(var_dataframe$politics_morality, na.rm = TRUE),
    sd_politics_morality = sd(var_dataframe$politics_morality, na.rm = TRUE),
    mean_public_tweets_morality = mean(var_dataframe$polinterest_morality, na.rm = TRUE),
    sd_public_tweets_morality = sd(var_dataframe$polinterest_morality, na.rm = TRUE)
  )
  
  return(result)
}



# Define the function
create_histograms_politics <- function() {
  emotional_appeals <- c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")
  new_names <- c("Positive Moral", "Negative Moral", "Positive Moral-Emotional", "Negative Moral-Emotional")
  
  plots <- list()
  
  for (i in seq_along(emotional_appeals)) {
    appeal <- emotional_appeals[i]
    name <- new_names[i]
    
    # Prepare the data
    newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
    newspaper_tweets_second <- newspaper_tweets %>% 
      filter(date >= "2017-09-25")
    
    politics_second <- politics %>% 
      filter(date >= "2017-09-25")
    
    polinterest$date <- as.Date(polinterest$created_at)
    polinterest_second <- polinterest %>% 
      filter(date >= "2017-09-25")
    
    newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[appeal]], list(date = newspaper_tweets_second$date), mean)
    colnames(newspaper_tweets_agg) <- c("date", "newspaper_tweets_morality")
    
    politics_agg <- aggregate(politics_second[[appeal]], list(date = politics_second$date), mean)
    colnames(politics_agg) <- c("date", "politics_morality")
    
    polinterest_agg <- aggregate(polinterest_second[[appeal]], list(date = polinterest_second$date), mean)
    colnames(polinterest_agg) <- c("date", "polinterest_morality")
    
    date_sequence <- seq(from = as.Date("2017-09-24"), to = as.Date("2022-01-01"), by = "day")
    var_dataframe <- tibble(date = date_sequence)
    
    var_dataframe <- merge(var_dataframe, newspaper_tweets_agg, all.x = TRUE)
    var_dataframe <- merge(var_dataframe, politics_agg, all.x = TRUE)
    var_dataframe <- merge(var_dataframe, polinterest_agg, all.x = TRUE)
    
    var_dataframe <- var_dataframe %>%
      mutate(week = floor_date(date, "week")) %>%
      group_by(week) %>%
      summarise(mean_value = mean(politics_morality, na.rm = TRUE))
    
    var_dataframe <- as.data.frame(var_dataframe)
    colnames(var_dataframe) <- c("week", "value")
    var_dataframe$appeal <- name
    
    # Generate the plot with title and white background
    plots[[i]] <- ggplot(var_dataframe, aes(x = value)) + 
      geom_histogram(bins = 30, fill = "grey", color = "black") + 
      facet_wrap(~ appeal, scales = "free") + 
      theme_minimal(base_size = 14) + 
      theme(plot.background = element_rect(fill = "white", colour = "white"),
            panel.background = element_rect(fill = "white", colour = "white"))
  }
  
  # Combine plots into a grid
  plot_grid(plotlist = plots, nrow = 2, ncol = 2, align = 'v')
  
}



create_histograms_public <- function() {
  emotional_appeals <- c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")
  new_names <- c("Positive Moral", "Negative Moral", "Positive Moral-Emotional", "Negative Moral-Emotional")
  
  plots <- list()
  
  for (i in seq_along(emotional_appeals)) {
    appeal <- emotional_appeals[i]
    name <- new_names[i]
    
    # Prepare the data
    newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
    newspaper_tweets_second <- newspaper_tweets %>% 
      filter(date >= "2017-09-25")
    
    politics_second <- politics %>% 
      filter(date >= "2017-09-25")
    
    polinterest$date <- as.Date(polinterest$created_at)
    polinterest_second <- polinterest %>% 
      filter(date >= "2017-09-25")
    
    newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[appeal]], list(date = newspaper_tweets_second$date), mean)
    colnames(newspaper_tweets_agg) <- c("date", "newspaper_tweets_morality")
    
    politics_agg <- aggregate(politics_second[[appeal]], list(date = politics_second$date), mean)
    colnames(politics_agg) <- c("date", "politics_morality")
    
    polinterest_agg <- aggregate(polinterest_second[[appeal]], list(date = polinterest_second$date), mean)
    colnames(polinterest_agg) <- c("date", "polinterest_morality")
    
    date_sequence <- seq(from = as.Date("2017-09-24"), to = as.Date("2022-01-01"), by = "day")
    var_dataframe <- tibble(date = date_sequence)
    
    var_dataframe <- merge(var_dataframe, newspaper_tweets_agg, all.x = TRUE)
    var_dataframe <- merge(var_dataframe, politics_agg, all.x = TRUE)
    var_dataframe <- merge(var_dataframe, polinterest_agg, all.x = TRUE)
    
    var_dataframe <- var_dataframe %>%
      mutate(week = floor_date(date, "week")) %>%
      group_by(week) %>%
      summarise(mean_value = mean(polinterest_morality, na.rm = TRUE))
    
    var_dataframe <- as.data.frame(var_dataframe)
    colnames(var_dataframe) <- c("week", "value")
    var_dataframe$appeal <- name
    
    # Generate the plot with title and white background
    plots[[i]] <- ggplot(var_dataframe, aes(x = value)) + 
      geom_histogram(bins = 30, fill = "grey", color = "black") + 
      facet_wrap(~ appeal, scales = "free") + 
      theme_minimal(base_size = 14) + 
      theme(plot.background = element_rect(fill = "white", colour = "white"),
            panel.background = element_rect(fill = "white", colour = "white"))
  }
  
  # Combine plots into a grid
  plot_grid(plotlist = plots, nrow = 2, ncol = 2, align = 'v')
}



run_adf_simple <- function(emotional_appeal, group, k) {
  
  newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
  newspaper_tweets_second <- newspaper_tweets |> 
    filter(date >= "2017-09-25")
  
  politics_second <- politics |> 
    filter(date >= "2017-09-25")
  
  polinterest$date <- as.Date(polinterest$created_at)
  polinterest_second <- polinterest |> 
    filter(date >= "2017-09-25")
  
  newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[emotional_appeal]], list(newspaper_tweets_second$date), mean)
  colnames(newspaper_tweets_agg)[1] <- "date"
  colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
  
  politics_agg <- aggregate(politics_second[[emotional_appeal]], list(politics_second$date), mean)
  colnames(politics_agg)[1] <- "date"
  colnames(politics_agg)[2] <- "politics_morality"
  
  polinterest_agg <- aggregate(polinterest_second[[emotional_appeal]], list(polinterest_second$date), mean)
  colnames(polinterest_agg)[1] <- "date"
  colnames(polinterest_agg)[2] <- "polinterest_morality"
  
  newspaper_tweets_agg <- newspaper_tweets_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  politics_agg <- politics_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  polinterest_agg <- polinterest_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  # Create a sequence of dates
  date_sequence <- seq(from = as.Date("2017-09-24"), to = as.Date("2022-01-01"), by = "day")
  
  # Create a dataframe using the date sequence
  var_dataframe <- tibble(date = date_sequence)
  
  var_dataframe <- merge(var_dataframe, newspaper_tweets_agg, all.x = TRUE)
  var_dataframe <- merge(var_dataframe, politics_agg, all.x = TRUE)
  var_dataframe <- merge(var_dataframe, polinterest_agg, all.x = TRUE)
  
  #set NAs to 0
  var_dataframe[is.na(var_dataframe)] <- 0
  var_dataframe$date <- NULL
  
  result <- adf.test(var_dataframe[[group]], k = k)
  
  return(result)
}


run_adf_tests_simple <- function() {
  # Define the parameters for the tests
  appeals <- c("morality_only_pos", "morality_only_neg", "neg_moremo", "pos_moremo")
  appeal_names <- c("Positive Moral", "Negative Moral", "Negative Moral-Emotional", "Positive Moral-Emotional")
  groups <- c("politics_morality", "newspaper_tweets_morality", "polinterest_morality")
  group_names <- c("Politics", "News Media", "Attentive Public")
  lags <- c(1, 4)
  
  # Initialize an empty data frame to store results
  results <- tibble(
    Appeal = character(),
    Actor_Group = character(),
    Lags = integer(),
    Statistic = numeric(),
    p_Value = character()
  )
  
  # Loop through all combinations of appeals, groups, and lags
  for (appeal in seq_along(appeals)) {
    for (group in seq_along(groups)) {
      for (lag in lags) {
        # Run the ADF test
        test_result <- run_adf_simple(appeals[appeal], groups[group], lag)
        
        # Extract Dickey-Fuller statistic and p-value
        df_statistic <- test_result$statistic['Dickey-Fuller']
        p_value <- ifelse(test_result$p.value < 0.01, "< 0.01", sprintf("%.2f", test_result$p.value))
        
        # Append results to the data frame
        results <- results %>% 
          add_row(
            Appeal = appeal_names[appeal],
            Actor_Group = group_names[group],
            Lags = lag,
            Statistic = df_statistic,
            p_Value = p_value
          )
      }
    }
  }
  
  # Return the results data frame
  return(results)
}



run_adf_complex <- function(emotional_appeal, group, k) {
  newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
  newspaper_tweets_second <- newspaper_tweets |> 
    filter(date >= "2017-09-25")
  
  politics_second <- politics |> 
    filter(date >= "2017-09-25")
  
  polinterest$date <- as.Date(polinterest$created_at)
  polinterest_second <- polinterest |> 
    filter(date >= "2017-09-25")
  
  partytype_agg <- aggregate(politics_second$morality_only, list(politics_second$date, politics_second$afd), mean)
  colnames(partytype_agg)[1] <- "date"
  colnames(partytype_agg)[2] <- "partytype"
  partytype_wide <- partytype_agg %>% spread(partytype, x)
  
  newspaper_tweets_agg <- aggregate(newspaper_tweets_second$morality_only, list(newspaper_tweets_second$date), mean)
  colnames(newspaper_tweets_agg)[1] <- "date"
  colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
  
  polinterest_agg <- aggregate(polinterest_second$morality_only, list(polinterest_second$date), mean)
  colnames(polinterest_agg)[1] <- "date"
  colnames(polinterest_agg)[2] <- "polinterest_morality"
  
  # Create a sequence of dates
  date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")
  
  # Create a dataframe using the date sequence
  var_complete <- tibble(date = date_sequence)
  
  var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
  var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
  var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)
  
  #set NAs to 0
  var_complete[is.na(var_complete)] <- 0
  var_complete$date <- NULL
  
  result <- adf.test(var_complete[[group]], k = k)
  
  return(result)
  
}

run_adf_tests_complex <- function() {
  # Define the parameters for the tests
  appeals <- c("morality_only_pos", "morality_only_neg", "neg_moremo", "pos_moremo")
  appeal_names <- c("Positive Moral", "Negative Moral", "Negative Moral-Emotional", "Positive Moral-Emotional")
  groups <- c("AfD", "Other Parties")
  lags <- c(1, 4)
  
  # Initialize an empty data frame to store results
  results <- tibble(
    Appeal = character(),
    Actor_Group = character(),
    Lags = integer(),
    Statistic = numeric(),
    p_Value = character()
  )
  
  # Loop through all combinations of appeals, groups, and lags
  for (appeal in seq_along(appeals)) {
    for (group in groups) {
      for (lag in lags) {
        # Run the ADF test
        test_result <- run_adf_complex(appeals[appeal], group, lag)
        
        # Extract Dickey-Fuller statistic and p-value
        df_statistic <- test_result$statistic['Dickey-Fuller']
        p_value <- ifelse(test_result$p.value < 0.01, "< 0.01", sprintf("%.2f", test_result$p.value))
        
        # Append results to the data frame
        results <- results %>% 
          add_row(
            Appeal = appeal_names[appeal],
            Actor_Group = group,
            Lags = lag,
            Statistic = df_statistic,
            p_Value = p_value
          )
      }
    }
  }
  
  # Return the results data frame
  return(results)
}



get_residual_plots_simple <- function(emotional_appeal, p) {
  newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
  newspaper_tweets_second <- newspaper_tweets |> 
    filter(date >= "2017-09-25")
  
  politics_second <- politics |> 
    filter(date >= "2017-09-25")
  
  polinterest$date <- as.Date(polinterest$created_at)
  polinterest_second <- polinterest |> 
    filter(date >= "2017-09-25")
  
  newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[emotional_appeal]], list(newspaper_tweets_second$date), mean)
  colnames(newspaper_tweets_agg)[1] <- "date"
  colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
  
  politics_agg <- aggregate(politics_second[[emotional_appeal]], list(politics_second$date), mean)
  colnames(politics_agg)[1] <- "date"
  colnames(politics_agg)[2] <- "politics_morality"
  
  polinterest_agg <- aggregate(polinterest_second[[emotional_appeal]], list(polinterest_second$date), mean)
  colnames(polinterest_agg)[1] <- "date"
  colnames(polinterest_agg)[2] <- "polinterest_morality"
  
  newspaper_tweets_agg <- newspaper_tweets_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  politics_agg <- politics_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  polinterest_agg <- polinterest_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  # Create a sequence of dates from August 15, 2015 to January 01, 2017
  date_sequence <- seq(from = as.Date("2017-09-24"), to = as.Date("2022-01-01"), by = "day")
  
  # Create a dataframe using the date sequence
  var_dataframe <- tibble(date = date_sequence)
  
  var_dataframe <- merge(var_dataframe, newspaper_tweets_agg, all.x = TRUE)
  var_dataframe <- merge(var_dataframe, politics_agg, all.x = TRUE)
  var_dataframe <- merge(var_dataframe, polinterest_agg, all.x = TRUE)
  
  #set NAs to 0
  var_dataframe[is.na(var_dataframe)] <- 0
  var_dataframe$date <- NULL
  
  # Mapping of 'appeal' values to titles
  title_map <- c(
    morality_only_pos = "1) Moral positive",
    morality_only_neg = "2) Moral negative",
    pos_moremo = "3) Moral-emotional positive",
    neg_moremo = "4) Moral-emotional negative"
  )
  plot_title <- title_map[emotional_appeal]
  
  
  var <- VAR(var_dataframe, p = p, type = "const")
  
  residuals_var <- residuals(var)
  
  
  residuals_long <- as.data.frame(residuals_var) %>%
    mutate(Time = 1:nrow(.)) %>%
    gather(key = "Series", value = "Residuals", -Time)
  
  residuals_long <- residuals_long %>%
    mutate(Series = str_replace_all(Series, c("newspaper_tweets_morality" = "newspaper", "polinterest_morality" = "public", "politics_morality" = "politics")))
  
  
  plot <- ggplot(residuals_long, aes(x = Time, y = Residuals, color = Series)) +
    geom_line(alpha = 0.3) +  # Make original lines semi-transparent
    geom_smooth(method = "loess", se = FALSE) +  # Add LOESS smoothed line
    theme_minimal() +
    labs(title = plot_title,
         x = "Time", y = "Residuals") +
    ylim(-0.2, 0.5)
  
  return(plot)
  
}



get_residual_plots_complex <- function(emotional_appeal, p_lags) {
  p <- p_lags
  newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
  newspaper_tweets_second <- newspaper_tweets |> 
    filter(date >= "2017-09-25")
  
  politics_second <- politics |> 
    filter(date >= "2017-09-25")
  
  polinterest$date <- as.Date(polinterest$created_at)
  polinterest_second <- polinterest |> 
    filter(date >= "2017-09-25")
  
  partytype_agg <- aggregate(politics_second[[emotional_appeal]], list(politics_second$date, politics_second$partytype), mean)
  colnames(partytype_agg)[1] <- "date"
  colnames(partytype_agg)[2] <- "partytype"
  partytype_wide <- partytype_agg %>% spread(partytype, x)
  
  newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[emotional_appeal]], list(newspaper_tweets_second$date), mean)
  colnames(newspaper_tweets_agg)[1] <- "date"
  colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
  
  polinterest_agg <- aggregate(polinterest_second[[emotional_appeal]], list(polinterest_second$date), mean)
  colnames(polinterest_agg)[1] <- "date"
  colnames(polinterest_agg)[2] <- "polinterest_morality"
  
  # Create a sequence of dates
  date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")
  
  # Create a dataframe using the date sequence
  var_complete <- tibble(date = date_sequence)
  
  var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
  var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
  var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)
  
  #set NAs to 0
  var_complete[is.na(var_complete)] <- 0
  var_complete$date <- NULL
  
  var <- VAR(var_complete, p = p, type = "const")
  
  residuals_var <- residuals(var)
  
  # Mapping of 'appeal' values to titles
  title_map <- c(
    morality_only_pos = "1) Moral positive",
    morality_only_neg = "2) Moral negative",
    pos_moremo = "3) Moral-emotional positive",
    neg_moremo = "4) Moral-emotional negative"
  )
  plot_title <- title_map[emotional_appeal]
  
  residuals_long <- as.data.frame(residuals_var) %>%
    mutate(Time = 1:nrow(.)) %>%
    gather(key = "Series", value = "Residuals", -Time)
  
  residuals_long <- residuals_long %>%
    mutate(Series = str_replace_all(Series, c("newspaper_tweets_morality" = "newspaper", "polinterest_morality" = "public", "politics_morality" = "politics")))
  
  
  plot <- ggplot(residuals_long, aes(x = Time, y = Residuals, color = Series)) +
    geom_line(alpha = 0.3) +  # Make original lines semi-transparent
    geom_smooth(method = "loess", se = FALSE) +  # Add LOESS smoothed line
    theme_minimal() +
    labs(title = plot_title,
         x = "Time", y = "Residuals") +
    ylim(-0.2, 0.5)
  
  return(plot)
  
} 




svar_complex_complete <- function(emotional_appeals, p_lags) {
  for (emotional_appeal in emotional_appeals) {
    p <- p_lags
    newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
    newspaper_tweets_second <- newspaper_tweets |> 
      filter(date >= "2017-09-25")
    
    politics_second <- politics |> 
      filter(date >= "2017-09-25")
    
    polinterest$date <- as.Date(polinterest$created_at)
    polinterest_second <- polinterest |> 
      filter(date >= "2017-09-25")
    
    partytype_agg <- aggregate(politics_second[[emotional_appeal]], list(politics_second$date, politics_second$partytype), mean)
    colnames(partytype_agg)[1] <- "date"
    colnames(partytype_agg)[2] <- "partytype"
    partytype_wide <- partytype_agg %>% spread(partytype, x)
    
    newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[emotional_appeal]], list(newspaper_tweets_second$date), mean)
    colnames(newspaper_tweets_agg)[1] <- "date"
    colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
    
    polinterest_agg <- aggregate(polinterest_second[[emotional_appeal]], list(polinterest_second$date), mean)
    colnames(polinterest_agg)[1] <- "date"
    colnames(polinterest_agg)[2] <- "polinterest_morality"
    
    # Create a sequence of dates
    date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")
    
    # Create a dataframe using the date sequence
    var_complete <- tibble(date = date_sequence)
    
    var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
    var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
    var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)
    
    #set NAs to 0
    var_complete[is.na(var_complete)] <- 0
    var_complete$date <- NULL
    
    var <- VAR(var_complete, p = p, type = "const")
    svar_model <- BQ(var)
    
    
    svar.13 <- irf(svar_model, n.ahead = 7,impulse = "radicalright", response = "polinterest_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.14 <- irf(svar_model, n.ahead = 7,impulse = "centerright", response = "polinterest_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.15 <- irf(svar_model, n.ahead = 7,impulse = "centerleft", response = "polinterest_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.16 <- irf(svar_model, n.ahead = 7,impulse = "radicalleft", response = "polinterest_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.17 <- irf(svar_model, n.ahead = 7,impulse = "polinterest_morality", response = "radicalright", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.18 <- irf(svar_model, n.ahead = 7,impulse = "polinterest_morality", response = "centerright", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.19 <- irf(svar_model, n.ahead = 7,impulse = "polinterest_morality", response = "centerleft", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.20 <- irf(svar_model, n.ahead = 7,impulse = "polinterest_morality", response = "radicalleft", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    
    # Initialize a data frame to store the results2
    results2 <- data.frame(IRF = numeric(), Coefficient = numeric(),
                           LowerBound = numeric(), UpperBound = numeric(),
                           Significant = logical(), Day = integer(),
                           ImpulseGroup = character(), ResponseGroup = character())
    
    # Number of IRFs
    num_irfs <- 8
    
    # Loop through all the IRFs
    for (i in 13:20) {
      # Get the IRF object
      irf_obj <- get(paste("svar", i, sep = "."))
      
      # Extract the impulse and response groups for this IRF
      impulse_group <- irf_obj$impulse
      response_group <- irf_obj$response
      
      # Extract the coefficients for days 1 to 7
      coef_days1to7 <- irf_obj$irf[[1]][1:8,]
      
      # Extract the lower and upper bounds for days 1 to 7
      lower_bounds_days1to7 <- irf_obj$Lower[[1]][1:8,]
      upper_bounds_days1to7 <- irf_obj$Upper[[1]][1:8,]
      
      # Check if the coefficients are significantly different from 0
      significant_days <- (lower_bounds_days1to7 > 0) | (upper_bounds_days1to7 < 0)
      
      # Filter significant coefficients
      significant_coefs <- coef_days1to7[significant_days]
      significant_lowers <- lower_bounds_days1to7[significant_days]
      significant_uppers <- upper_bounds_days1to7[significant_days]
      
      if (length(significant_coefs) > 0) {
        if (all(significant_coefs >= 0)) {
          index <- which.max(significant_coefs)
        } else if (all(significant_coefs <= 0)) {
          index <- which.min(significant_coefs)
        } else {
          index <- which.max(abs(significant_coefs))
        }
        
        # Add the results2 to the data frame
        results2 <- rbind(results2, data.frame(IRF = i, Coefficient = significant_coefs[index],
                                               LowerBound = significant_lowers[index], UpperBound = significant_uppers[index],
                                               Significant = TRUE, Day = which(significant_days)[index],
                                               ImpulseGroup = impulse_group, ResponseGroup = response_group))
      } else {
        index <- which.max(abs(coef_days1to7))
        results2 <- rbind(results2, data.frame(IRF = i, Coefficient = coef_days1to7[index],
                                               LowerBound = lower_bounds_days1to7[index], UpperBound = upper_bounds_days1to7[index],
                                               Significant = FALSE, Day = index,
                                               ImpulseGroup = impulse_group, ResponseGroup = response_group))
      }
    }
    
    results2$appeal <- emotional_appeal
    # Display the results2
    complex_complete <- rbind(complex_complete, results2)
    
    
  }
  
  return(complex_complete)
}



svar_full <- function(emotional_appeals, p_lags) {
  for (emotional_appeal in emotional_appeals) {
    p <- p_lags
    newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
    newspaper_tweets_second <- newspaper_tweets |> 
      filter(date >= "2017-09-25")
    
    politics_second <- politics |> 
      filter(date >= "2017-09-25")
    
    polinterest$date <- as.Date(polinterest$created_at)
    polinterest_second <- polinterest |> 
      filter(date >= "2017-09-25")
    
    partytype_agg <- aggregate(politics_second[[emotional_appeal]], list(politics_second$date, politics_second$partytype), mean)
    colnames(partytype_agg)[1] <- "date"
    colnames(partytype_agg)[2] <- "partytype"
    partytype_wide <- partytype_agg %>% spread(partytype, x)
    
    newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[emotional_appeal]], list(newspaper_tweets_second$date), mean)
    colnames(newspaper_tweets_agg)[1] <- "date"
    colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
    
    polinterest_agg <- aggregate(polinterest_second[[emotional_appeal]], list(polinterest_second$date), mean)
    colnames(polinterest_agg)[1] <- "date"
    colnames(polinterest_agg)[2] <- "polinterest_morality"
    
    # Create a sequence of dates
    date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")
    
    # Create a dataframe using the date sequence
    var_complete <- tibble(date = date_sequence)
    
    var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
    var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
    var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)
    
    #set NAs to 0
    var_complete[is.na(var_complete)] <- 0
    var_complete$date <- NULL
    
    var <- VAR(var_complete, p = p, type = "const")
    svar_model <- BQ(var)
    
    
    svar.1 <- irf(svar_model, impulse = "radicalright", response = "centerright", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.2 <- irf(svar_model, impulse = "radicalright", response = "centerleft", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.3 <- irf(svar_model, impulse = "radicalright", response = "radicalleft", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    
    svar.4 <- irf(svar_model, impulse = "centerright", response = "radicalright", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.5 <- irf(svar_model, impulse = "centerright", response = "centerleft", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.6 <- irf(svar_model, impulse = "centerright", response = "radicalleft", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    
    svar.7 <- irf(svar_model, impulse = "centerleft", response = "radicalright", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.8 <- irf(svar_model, impulse = "centerleft", response = "centerright", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.9 <- irf(svar_model, impulse = "centerleft", response = "radicalleft", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    
    svar.10 <- irf(svar_model, impulse = "radicalleft", response = "radicalright", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.11 <- irf(svar_model, impulse = "radicalleft", response = "centerright", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.12 <- irf(svar_model, impulse = "radicalleft", response = "centerleft", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    

    svar.13 <- irf(svar_model, impulse = "radicalright", response = "polinterest_morality", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.14 <- irf(svar_model, impulse = "centerright", response = "polinterest_morality", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.15 <- irf(svar_model, impulse = "centerleft", response = "polinterest_morality", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.16 <- irf(svar_model, impulse = "radicalleft", response = "polinterest_morality", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    
    
    svar.17 <- irf(svar_model, impulse = "polinterest_morality", response = "radicalright", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.18 <- irf(svar_model, impulse = "polinterest_morality", response = "centerright", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.19 <- irf(svar_model, impulse = "polinterest_morality", response = "centerleft", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.20 <- irf(svar_model, impulse = "polinterest_morality", response = "radicalleft", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    
    
    svar.21 <- irf(svar_model, impulse = "radicalright", response = "newspaper_tweets_morality", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.22 <- irf(svar_model, impulse = "centerright", response = "newspaper_tweets_morality", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.23 <- irf(svar_model, impulse = "centerleft", response = "newspaper_tweets_morality", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.24 <- irf(svar_model, impulse = "radicalleft", response = "newspaper_tweets_morality", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    
    
    svar.25 <- irf(svar_model, impulse = "newspaper_tweets_morality", response = "polinterest_morality", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.26 <- irf(svar_model, impulse = "newspaper_tweets_morality", response = "radicalright", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.27 <- irf(svar_model, impulse = "newspaper_tweets_morality", response = "centerright", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    svar.28 <- irf(svar_model, impulse = "newspaper_tweets_morality", response = "centerleft", n.ahead = 7, ortho = TRUE, cumulative = TRUE, seed = 1241, runs = 1000)
    
    
    # Initialize a data frame to store the results2
    results2 <- data.frame(IRF = numeric(), Coefficient = numeric(),
                           LowerBound = numeric(), UpperBound = numeric(),
                           Significant = logical(), Day = integer(),
                           ImpulseGroup = character(), ResponseGroup = character())
    
    # Number of IRFs
    num_irfs <- 28
    
    # Loop through all the IRFs
    for (i in 1:28) {
      # Get the IRF object
      irf_obj <- get(paste("svar", i, sep = "."))
      
      # Extract the impulse and response groups for this IRF
      impulse_group <- irf_obj$impulse
      response_group <- irf_obj$response
      
      # Extract the coefficients for days 1 to 7
      coef_days1to7 <- irf_obj$irf[[1]][1:8,]
      
      # Extract the lower and upper bounds for days 1 to 7
      lower_bounds_days1to7 <- irf_obj$Lower[[1]][1:8,]
      upper_bounds_days1to7 <- irf_obj$Upper[[1]][1:8,]
      
      # Check if the coefficients are significantly different from 0
      significant_days <- (lower_bounds_days1to7 > 0) | (upper_bounds_days1to7 < 0)
      
      # Filter significant coefficients
      significant_coefs <- coef_days1to7[significant_days]
      significant_lowers <- lower_bounds_days1to7[significant_days]
      significant_uppers <- upper_bounds_days1to7[significant_days]
      
      if (length(significant_coefs) > 0) {
        if (all(significant_coefs >= 0)) {
          index <- which.max(significant_coefs)
        } else if (all(significant_coefs <= 0)) {
          index <- which.min(significant_coefs)
        } else {
          index <- which.max(abs(significant_coefs))
        }
        
        # Add the results2 to the data frame
        results2 <- rbind(results2, data.frame(IRF = i, Coefficient = significant_coefs[index],
                                               LowerBound = significant_lowers[index], UpperBound = significant_uppers[index],
                                               Significant = TRUE, Day = which(significant_days)[index],
                                               ImpulseGroup = impulse_group, ResponseGroup = response_group))
      } else {
        # If no significant coefficients are found, add the row with the largest absolute coefficient
        index <- which.max(abs(coef_days1to7))
        results2 <- rbind(results2, data.frame(IRF = i, Coefficient = coef_days1to7[index],
                                               LowerBound = lower_bounds_days1to7[index], UpperBound = upper_bounds_days1to7[index],
                                               Significant = FALSE, Day = index,
                                               ImpulseGroup = impulse_group, ResponseGroup = response_group))
      }
    }
    
    results2$appeal <- emotional_appeal
    # Display the results2
    complex_full <- rbind(complex_full, results2)
    
    
  }
  
  return(complex_full)
}


create_coef_plot_complex_complete <- function(results) {
  
  results$Source <- "Day 7"
  combined_results <- results

  filtered_results <- combined_results[combined_results$IRF >= 13 & combined_results$IRF <= 16, ]
  
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="polinterest_morality"] <- "polinterest"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="polinterest_morality"] <- "polinterest"
  
  # Set the order for ImpulseGroup
  filtered_results$ImpulseGroup <- factor(filtered_results$ImpulseGroup, 
                                          levels = rev(c("polinterest", "radicalright", "centerright", "centerleft", "radicalleft")), 
                                          labels = rev(c("Public", "Radical Right", "Center Right", "Center Left", "Radical Left")))
  
  # Set the order for ResponseGroup
  filtered_results$ResponseGroup <- factor(filtered_results$ResponseGroup, 
                                           levels = rev(c("polinterest", "radicalright", "centerright", "centerleft", "radicalleft")), 
                                           labels = rev(c("Public", "Radical Right", "Center Right", "Center Left", "Radical Left")))
  
  
  # Set the order for appeals
  filtered_results$appeal <- factor(filtered_results$appeal, 
                                    levels = rev(c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")), 
                                    labels = rev(c("M Positive", "M Negative", "ME Positive", "ME Negative")))
  
  # Assuming 'impulse_group' and 'response_group' are columns in filtered_results
  filtered_results$ImpulseGroup <- as.factor(filtered_results$ImpulseGroup)
  filtered_results$ResponseGroup <- as.factor(filtered_results$ResponseGroup)
  filtered_results$appeal <- as.factor(filtered_results$appeal)
  
  # Change 'Significant' from logical to character to prevent issues with ggplot
  filtered_results$Significant <- as.character(filtered_results$Significant)
  
  # Mapping of 'appeal' values to titles
  title_map <- c(
    morality_only_pos = "1) Moral positive",
    morality_only_neg = "2) Moral negative",
    pos_moremo = "3) Moral-emotional positive",
    neg_moremo = "4) Moral-emotional negative"
  )
  plot_title <- title_map[filtered_results$appeal[1]]
  
  # Plot
  coef_plot1 <- ggplot(filtered_results, aes(y = ImpulseGroup, x = Coefficient, group = appeal)) +
    geom_linerange(aes(xmin = Coefficient, xmax = UpperBound), position = position_dodge(width = 0.5), colour = "black") +
    geom_linerange(aes(xmin = LowerBound, xmax = Coefficient), position = position_dodge(width = 0.5), colour = "black") +
    geom_point(aes(shape = appeal), position = position_dodge(width = 0.5), colour = "black", size = 2) +
    geom_vline(xintercept = 0, linetype = "dashed", color = "red", size = 0.3) +
    xlim(-0.05, 0.1) +
    facet_wrap(~ResponseGroup, ncol = 5) +
    theme_minimal() +
    theme(axis.title.x = element_blank(),  # Remove x-axis label
          axis.title.y = element_blank(),
          text = element_text(size = 14),
          legend.position = "none") +
    scale_shape_manual(values = c(15, 17, 18, 20), name = "Appeal") +
    guides(shape = guide_legend(reverse = TRUE))
  
  filtered_results <- combined_results[combined_results$IRF >= 17 & combined_results$IRF <= 20, ]
  
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="polinterest_morality"] <- "polinterest"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="polinterest_morality"] <- "polinterest"
  
  
  # Set the order for ImpulseGroup
  filtered_results$ImpulseGroup <- factor(filtered_results$ImpulseGroup, 
                                          levels = rev(c("polinterest", "radicalright", "centerright", "centerleft", "radicalleft")), 
                                          labels = rev(c("Public", "Radical Right", "Center Right", "Center Left", "Radical Left")))
  
  # Set the order for ResponseGroup
  filtered_results$ResponseGroup <- factor(filtered_results$ResponseGroup, 
                                           levels = rev(c("polinterest", "radicalright", "centerright", "centerleft", "radicalleft")), 
                                           labels = rev(c("Public", "Radical Right", "Center Right", "Center Left", "Radical Left")))
  
  
  # Set the order for appeals
  filtered_results$appeal <- factor(filtered_results$appeal, 
                                    levels = rev(c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")), 
                                    labels = rev(c("M Positive", "M Negative", "ME Positive", "ME Negative")))
  
  
  
  # Assuming 'impulse_group' and 'response_group' are columns in filtered_results
  filtered_results$ImpulseGroup <- as.factor(filtered_results$ImpulseGroup)
  filtered_results$ResponseGroup <- as.factor(filtered_results$ResponseGroup)
  filtered_results$appeal <- as.factor(filtered_results$appeal)
  
  
  # Change 'Significant' from logical to character to prevent issues with ggplot
  filtered_results$Significant <- as.character(filtered_results$Significant)
  
  # Assuming 'ResponseGroup' is the faceting variable
  filtered_results$ResponseGroup <- factor(filtered_results$ResponseGroup, 
                                           levels = c("Radical Right", "Center Right", "Center Left", "Radical Left"))
  
  
  # Plot
  coef_plot2 <- ggplot(filtered_results, aes(y = ImpulseGroup, x = Coefficient, group = appeal)) +
    geom_linerange(aes(xmin = Coefficient, xmax = UpperBound), position = position_dodge(width = 0.5), colour = "black") +
    geom_linerange(aes(xmin = LowerBound, xmax = Coefficient), position = position_dodge(width = 0.5), colour = "black") +
    geom_point(aes(shape = appeal), position = position_dodge(width = 0.5), colour = "black", size = 2) +
    geom_vline(xintercept = 0, linetype = "dashed", color = "red", size = 0.3) +
    xlim(-0.05, 0.1) +
    facet_wrap(~ResponseGroup, nrow = 5) +
    theme_minimal() +
    theme(axis.title.x = element_blank(),  # Remove x-axis label
          axis.title.y = element_blank(),
          text = element_text(size = 14)) +
    scale_shape_manual(values = c(15, 17, 18, 20), name = "Appeal") +
    guides(shape = guide_legend(reverse = TRUE))
  
  # Combine the two plots
  plot <- plot_grid(coef_plot1, coef_plot2, ncol = 2, rel_heights = 4)

  return(plot)
}


create_coef_plot_complex_full <- function(results) {
  
  
  # Combine the results data frames and add a new column to identify the source of the coefficients
  results$Source <- "Day 7"
  results <- results %>%
    filter(ImpulseGroup != "newspaper_tweets_morality")
  results <- results %>%
    filter(ResponseGroup != "newspaper_tweets_morality")
  combined_results <- results
  
  # Filter the combined results to obtain relevant groups to include only IRFs 1-16
  filtered_results <- combined_results
  
  filtered_results$ImpulseGroup[filtered_results$ImpulseGroup=="polinterest_morality"] <- "polinterest"
  filtered_results$ResponseGroup[filtered_results$ResponseGroup=="polinterest_morality"] <- "polinterest"
  
  # Set the order for ImpulseGroup
  filtered_results$ImpulseGroup <- factor(filtered_results$ImpulseGroup, 
                                          levels = rev(c("polinterest", "radicalright", "centerright", "centerleft", "radicalleft")), 
                                          labels = rev(c("Public", "Radical Right", "Center Right", "Center Left", "Radical Left")))
  
  # Set the order for ResponseGroup
  filtered_results$ResponseGroup <- factor(filtered_results$ResponseGroup, 
                                           levels = rev(c("polinterest", "radicalright", "centerright", "centerleft", "radicalleft")), 
                                           labels = rev(c("Public", "Radical Right", "Center Right", "Center Left", "Radical Left")))
  
  
  # Set the order for ResponseGroup
  filtered_results$appeal <- factor(filtered_results$appeal, 
                                    levels = rev(c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")), 
                                    labels = rev(c("M positive", "M negative", "ME positive", "ME negative")))
  
  # Assuming 'impulse_group' and 'response_group' are columns in filtered_results
  filtered_results$ImpulseGroup <- as.factor(filtered_results$ImpulseGroup)
  filtered_results$ResponseGroup <- as.factor(filtered_results$ResponseGroup)
  filtered_results$appeal <- as.factor(filtered_results$appeal)
  
  # Change 'Significant' from logical to character to prevent issues with ggplot
  filtered_results$Significant <- as.character(filtered_results$Significant)
  
  # Mapping of 'appeal' values to titles
  title_map <- c(
    morality_only_pos = "1) Moral positive",
    morality_only_neg = "2) Moral negative",
    pos_moremo = "3) Moral-emotional positive",
    neg_moremo = "4) Moral-emotional negative"
  )
  plot_title <- title_map[filtered_results$appeal[1]]
  
  # Plot
  coef_plot1 <- ggplot(filtered_results, aes(y = ImpulseGroup, x = Coefficient, group = appeal)) +
    geom_linerange(aes(xmin = Coefficient, xmax = UpperBound), position = position_dodge(width = 0.5), colour = "black") +
    geom_linerange(aes(xmin = LowerBound, xmax = Coefficient), position = position_dodge(width = 0.5), colour = "black") +
    geom_point(aes(shape = appeal), position = position_dodge(width = 0.5), colour = "black", size = 2) +
    geom_vline(xintercept = 0, linetype = "dashed", color = "red", size = 0.3) +
    xlim(-0.05, 0.1) +
    facet_wrap(~ResponseGroup, ncol = 5) +
    theme_minimal() +
    theme(axis.title.x = element_blank(),  # Remove x-axis label
          axis.title.y = element_blank(),
          text = element_text(size = 14)) +
    scale_shape_manual(values = c(15, 17, 18, 20), name = "Appeal") +
    guides(shape = guide_legend(reverse = TRUE))
  
  # Combine the two plots
  plot <- plot_grid(coef_plot1, ncol = 1, rel_heights = 4)

  return(plot)
}



svar_simple_withoutBild <- function(emotional_appeal, p_lags) {
  p <- p_lags
  newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
  newspaper_tweets_second <- newspaper_tweets |> 
    filter(date >= "2017-09-25")
  newspaper_tweets_third <- newspaper_tweets_second %>%
    filter(author_id != "9204502")
  
  politics_second <- politics |> 
    filter(date >= "2017-09-25")
  
  polinterest$date <- as.Date(polinterest$created_at)
  polinterest_second <- polinterest |> 
    filter(date >= "2017-09-25")
  
  newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[emotional_appeal]], list(newspaper_tweets_second$date), mean)
  colnames(newspaper_tweets_agg)[1] <- "date"
  colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
  
  politics_agg <- aggregate(politics_second[[emotional_appeal]], list(politics_second$date), mean)
  colnames(politics_agg)[1] <- "date"
  colnames(politics_agg)[2] <- "politics_morality"
  
  polinterest_agg <- aggregate(polinterest_second[[emotional_appeal]], list(polinterest_second$date), mean)
  colnames(polinterest_agg)[1] <- "date"
  colnames(polinterest_agg)[2] <- "polinterest_morality"
  
  newspaper_tweets_agg <- newspaper_tweets_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  politics_agg <- politics_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  polinterest_agg <- polinterest_agg |>
    filter(date > "2017-09-24" & date < "2022-01-01")
  
  # Create a sequence of dates from August 15, 2015 to January 01, 2017
  date_sequence <- seq(from = as.Date("2017-09-24"), to = as.Date("2022-01-01"), by = "day")
  
  # Create a dataframe using the date sequence
  var_dataframe <- tibble(date = date_sequence)
  
  var_dataframe <- merge(var_dataframe, newspaper_tweets_agg, all.x = TRUE)
  var_dataframe <- merge(var_dataframe, politics_agg, all.x = TRUE)
  var_dataframe <- merge(var_dataframe, polinterest_agg, all.x = TRUE)
  
  #set NAs to 0
  var_dataframe[is.na(var_dataframe)] <- 0
  var_dataframe$date <- NULL
  
  var <- VAR(var_dataframe, p = p, type = "const")
  svar_model <- BQ(var)
  
  svar.1 <- irf(svar_model, n.ahead = 7,impulse = "newspaper_tweets_morality", response = "polinterest_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
  
  svar.2 <- irf(svar_model, n.ahead = 7,impulse = "newspaper_tweets_morality", response = "politics_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
  
  svar.3 <- irf(svar_model, n.ahead = 7,impulse = "politics_morality", response = "polinterest_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)  
  
  svar.4 <- irf(svar_model, n.ahead = 7,impulse = "politics_morality", response = "newspaper_tweets_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)  
  
  svar.5 <- irf(svar_model, n.ahead = 7,impulse = "polinterest_morality", response = "politics_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)  
  
  svar.6 <- irf(svar_model, n.ahead = 7,impulse = "polinterest_morality", response = "newspaper_tweets_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)  
  
  
  # Initialize a data frame to store the results2
  results2 <- data.frame(IRF = numeric(), Coefficient = numeric(),
                         LowerBound = numeric(), UpperBound = numeric(),
                         Significant = logical(), Day = integer(),
                         ImpulseGroup = character(), ResponseGroup = character())
  
  # Number of IRFs
  num_irfs <- 6
  
  # Loop through all the IRFs
  for (i in 1:num_irfs) {
    # Get the IRF object
    irf_obj <- get(paste("svar", i, sep = "."))
    
    # Extract the impulse and response groups for this IRF
    impulse_group <- irf_obj$impulse
    response_group <- irf_obj$response
    
    # Extract the coefficients for days 1 to 7
    coef_days1to7 <- irf_obj$irf[[1]][1:8,]
    
    # Extract the lower and upper bounds for days 1 to 7
    lower_bounds_days1to7 <- irf_obj$Lower[[1]][1:8,]
    upper_bounds_days1to7 <- irf_obj$Upper[[1]][1:8,]
    
    # Check if the coefficients are significantly different from 0
    significant_days <- (lower_bounds_days1to7 > 0) | (upper_bounds_days1to7 < 0)
    
    # Filter significant coefficients
    significant_coefs <- coef_days1to7[significant_days]
    significant_lowers <- lower_bounds_days1to7[significant_days]
    significant_uppers <- upper_bounds_days1to7[significant_days]
    
    if (length(significant_coefs) > 0) {
      if (all(significant_coefs >= 0)) {
        index <- which.max(significant_coefs)
      } else if (all(significant_coefs <= 0)) {
        index <- which.min(significant_coefs)
      } else {
        index <- which.max(abs(significant_coefs))
      }
      
      # Add the results2 to the data frame
      results2 <- rbind(results2, data.frame(IRF = i, Coefficient = significant_coefs[index],
                                             LowerBound = significant_lowers[index], UpperBound = significant_uppers[index],
                                             Significant = TRUE, Day = which(significant_days)[index],
                                             ImpulseGroup = impulse_group, ResponseGroup = response_group))
    } else {
      # If no significant coefficients are found, add the row with the largest absolute coefficient
      index <- which.max(abs(coef_days1to7))
      results2 <- rbind(results2, data.frame(IRF = i, Coefficient = coef_days1to7[index],
                                             LowerBound = lower_bounds_days1to7[index], UpperBound = upper_bounds_days1to7[index],
                                             Significant = FALSE, Day = index,
                                             ImpulseGroup = impulse_group, ResponseGroup = response_group))
    }
  }
  
  # Display the results2
  return(results2)
}



svar_complex_complete_binary_withoutBild <- function(emotional_appeals, p_lags) {
  for (emotional_appeal in emotional_appeals) {
    p <- p_lags
    newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
    newspaper_tweets_second <- newspaper_tweets |> 
      filter(date >= "2017-09-25")
    newspaper_tweets_third <- newspaper_tweets_second %>%
      filter(author_id != "9204502")
    
    politics_second <- politics |> 
      filter(date >= "2017-09-25")
    
    polinterest$date <- as.Date(polinterest$created_at)
    polinterest_second <- polinterest |> 
      filter(date >= "2017-09-25")
    
    partytype_agg <- aggregate(politics_second[[emotional_appeal]], list(politics_second$date, politics_second$afd), mean)
    colnames(partytype_agg)[1] <- "date"
    colnames(partytype_agg)[2] <- "partytype"
    partytype_wide <- partytype_agg %>% spread(partytype, x)
    
    newspaper_tweets_agg <- aggregate(newspaper_tweets_second[[emotional_appeal]], list(newspaper_tweets_second$date), mean)
    colnames(newspaper_tweets_agg)[1] <- "date"
    colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"
    
    polinterest_agg <- aggregate(polinterest_second[[emotional_appeal]], list(polinterest_second$date), mean)
    colnames(polinterest_agg)[1] <- "date"
    colnames(polinterest_agg)[2] <- "polinterest_morality"
    
    # Create a sequence of dates from August 15, 2015 to January 01, 2017
    date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")
    
    # Create a dataframe using the date sequence
    var_complete <- tibble(date = date_sequence)
    
    var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
    var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
    var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)
    
    #set NAs to 0
    var_complete[is.na(var_complete)] <- 0
    var_complete$date <- NULL
    
    var <- VAR(var_complete, p = p, type = "const")
    svar_model <- BQ(var)
    
    
    svar.13 <- irf(svar_model, n.ahead = 7,impulse = "AfD", response = "polinterest_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.14 <- irf(svar_model, n.ahead = 7,impulse = "Other.Parties", response = "polinterest_morality", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.15 <- irf(svar_model, n.ahead = 7,impulse = "polinterest_morality", response = "AfD", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    svar.16 <- irf(svar_model, n.ahead = 7,impulse = "polinterest_morality", response = "Other.Parties", boot = TRUE, cum = TRUE, seed = 1241, runs = 1000)
    
    
    # Initialize a data frame to store the results2
    results2 <- data.frame(IRF = numeric(), Coefficient = numeric(),
                           LowerBound = numeric(), UpperBound = numeric(),
                           Significant = logical(), Day = integer(),
                           ImpulseGroup = character(), ResponseGroup = character())
    
    # Number of IRFs
    num_irfs <- 4
    
    # Loop through all the IRFs
    for (i in 13:16) {
      # Get the IRF object
      irf_obj <- get(paste("svar", i, sep = "."))
      
      # Extract the impulse and response groups for this IRF
      impulse_group <- irf_obj$impulse
      response_group <- irf_obj$response
      
      # Extract the coefficients for days 1 to 7
      coef_days1to7 <- irf_obj$irf[[1]][1:8,]
      
      # Extract the lower and upper bounds for days 1 to 7
      lower_bounds_days1to7 <- irf_obj$Lower[[1]][1:8,]
      upper_bounds_days1to7 <- irf_obj$Upper[[1]][1:8,]
      
      # Check if the coefficients are significantly different from 0
      significant_days <- (lower_bounds_days1to7 > 0) | (upper_bounds_days1to7 < 0)
      
      # Filter significant coefficients
      significant_coefs <- coef_days1to7[significant_days]
      significant_lowers <- lower_bounds_days1to7[significant_days]
      significant_uppers <- upper_bounds_days1to7[significant_days]
      
      if (length(significant_coefs) > 0) {
        if (all(significant_coefs >= 0)) {
          index <- which.max(significant_coefs)
        } else if (all(significant_coefs <= 0)) {
          index <- which.min(significant_coefs)
        } else {
          index <- which.max(abs(significant_coefs))
        }
        
        # Add the results2 to the data frame
        results2 <- rbind(results2, data.frame(IRF = i, Coefficient = significant_coefs[index],
                                               LowerBound = significant_lowers[index], UpperBound = significant_uppers[index],
                                               Significant = TRUE, Day = which(significant_days)[index],
                                               ImpulseGroup = impulse_group, ResponseGroup = response_group))
      } else {
        # If no significant coefficients are found, add the row with the largest absolute coefficient
        index <- which.max(abs(coef_days1to7))
        results2 <- rbind(results2, data.frame(IRF = i, Coefficient = coef_days1to7[index],
                                               LowerBound = lower_bounds_days1to7[index], UpperBound = upper_bounds_days1to7[index],
                                               Significant = FALSE, Day = index,
                                               ImpulseGroup = impulse_group, ResponseGroup = response_group))
      }
    }
    
    results2$appeal <- emotional_appeal
    # Display the results2
    complex_complete <- rbind(complex_complete, results2)
    
    
  }
  
  return(complex_complete)
}


